package com.some;

import java.util.Scanner;

public class Boss01_3 {
    private static int[][] f = new int[32][32];
    private static int[][] root = new int[32][32];

    public static void dealTree(int[] tree, int n) {
        for (int len = 1; len <= n; len++) {
            for (int i = 1; i + len - 1 <= n; i++) {
                int j = i + len - 1;
                for (int k = i; k <= j; k++) {
                    int left = k == i ? 1 : f[i][k - 1], right = k == j ? 1 : f[k + 1][j];
                    int score = left * right + tree[k];
                    if (i == j) score = tree[k];
                    if (score > f[i][j]) {
                        f[i][j] = score;
                        root[i][j] = k;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] tree = new int[n + 1];
        for (int i = 1; i <= n; i++) tree[i] = sc.nextInt();

        dealTree(tree, n);

        System.out.println(f[1][n]);
        System.out.println(dfs(1, n));
    }

    public static String dfs(int left, int right) {
        if (left > right) return "";
        int tmp = root[left][right];
        return tmp + " " + dfs(left, tmp - 1) + dfs(tmp + 1, right);
    }
}
